package com.gem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.entity.Order;
import com.gem.entity.OrderItem;
import com.gem.entity.UserAddress;
import com.gem.mapper.OrderItemMapper;
import com.gem.mapper.OrderMapper;
import com.gem.mapper.UserAddressMapper;
import com.gem.service.OrderService;
import com.gem.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘聪
 * Created  on 2021/4/21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper,Order> implements OrderService {


    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper itemMapper;
    @Autowired
    private UserAddressMapper addressMapper;
    @Override
    public List<OrderVO> orderList(Integer id) {
        List<OrderVO> voList = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", id);
        List<Order> orderList = (List<Order>) orderMapper.selectList(wrapper);
        for (Order order : orderList) {
            OrderVO orderVO = new OrderVO();
            QueryWrapper itemWrapper = new QueryWrapper();
            itemWrapper.eq("order_no", order.getOrderNo());
            List<OrderItem> itemList = (List<OrderItem>) itemMapper.selectList(itemWrapper);
            BeanUtils.copyProperties(order,orderVO);
            orderVO.setItemList(itemList);
            voList.add(orderVO);
        }

        return voList;
    }

    @Override
    public List<OrderVO> selectAll(List<Order> orderList) {
        List<OrderVO> voList = new ArrayList<>();

        for (Order order : orderList) {
            OrderVO orderVO = new OrderVO();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id",order.getShippingId());
            UserAddress address = addressMapper.selectOne(queryWrapper);
            BeanUtils.copyProperties(order,orderVO);
            BeanUtils.copyProperties(address,orderVO);
            voList.add(orderVO);
        }

        return voList;
    }
}
